//
//  ShopTableViewCell.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/6/20.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "ShopTableViewCell.h"
#import <QuartzCore/QuartzCore.h>

@interface ShopTableViewCell ()
{
    NSString * i ;
}
@property (nonatomic,strong) MBProgressHUD *hub;
@end

@implementation ShopTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _imageViewHeader.layer.masksToBounds = YES;
    _imageViewHeader.layer.cornerRadius = 10;
    
    _viewContent.layer.masksToBounds = YES;
    _viewContent.layer.cornerRadius = 10;
    
    _viewContent.layer.borderColor = [kColorFrom0x(0xf5f5f5) CGColor];
    _viewContent.layer.borderWidth = 2.f;
    
    UITapGestureRecognizer *tapRecognizerTel=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callFun)];
    
    _labelMerTel.userInteractionEnabled=YES;
    [_labelMerTel addGestureRecognizer:tapRecognizerTel];
}

- (void)showDataWithModel:(ShopModel *)model{
    _labelShopName.text = model.name;
    
    _shopAddress.text = [NSString stringWithFormat:@"%@%@",model.area_name,model.specific_address];
    
    _labelMerPerson.text = model.contact;
    
//    [_labelMerTel setTitle:model.tel forState:UIControlStateNormal];
    _labelMerTel.text = model.tel;
    
    [self creatNewImageViewAndLabels];
    
    _labelAllNumMy.text = [NSString stringWithFormat:@"%ld",model.all_num.integerValue];
    
//     i = @"1";
    
//    model.online_num = [NSNull new];
    
    if (![BGFunctionHelper isNULLOfString:model.online_num]) {
        if (model.online_num.integerValue == 0) {
            _labelOnlineNumMy.hidden = YES;
            _imageViewOnlineNumMy.hidden = YES;
            
            _labelLanyaMy.frame = _labelMimaMy.frame;
            _imageViewLanyaMy.frame = _imageViewMimaMy.frame;
            
            _labelMimaMy.frame = _labelOfflineNumMy.frame;
            _imageViewMimaMy.frame = _imageViewOfflineNumMy.frame;
            
            _labelOfflineNumMy.frame = _labelOnlineNumMy.frame;
            _imageViewOfflineNumMy.frame = _imageViewOnlineNumMy.frame;
            
        }else{

            _labelOnlineNumMy.hidden = NO;
            _imageViewOnlineNumMy.hidden = NO;
            
            _labelMimaMy.frame = _labelMimaMy.frame;
            _imageViewMimaMy.frame = _imageViewMimaMy.frame;
            
            _labelOfflineNumMy.frame = _labelOfflineNumMy.frame;
            _imageViewOfflineNumMy.frame = _imageViewOfflineNumMy.frame;
            
            _labelLanyaMy.frame = _labelLanyaMy.frame;
            _imageViewLanyaMy.frame = _imageViewLanyaMy.frame;
            
            _labelOnlineNumMy.text = [NSString stringWithFormat:@"%ld",model.online_num.integerValue];
        }
    }
   
    if (![BGFunctionHelper isNULLOfString:model.offine_num]) {
        if ( model.offine_num.integerValue == 0) {
            _labelOfflineNumMy.hidden = YES;
            _imageViewOfflineNumMy.hidden = YES;
            
            _labelLanyaMy.frame = _labelMimaMy.frame;
            _imageViewLanyaMy.frame = _imageViewMimaMy.frame;
            
            _labelMimaMy.frame = _labelOfflineNumMy.frame;
            _imageViewMimaMy.frame = _imageViewOfflineNumMy.frame;
            
        }else{
            
            _labelOfflineNumMy.hidden = NO;
            _imageViewOfflineNumMy.hidden = NO;
            
            _labelMimaMy.frame = _labelMimaMy.frame;
            _imageViewMimaMy.frame = _imageViewMimaMy.frame;
            
            _labelLanyaMy.frame = _labelLanyaMy.frame;
            _imageViewLanyaMy.frame = _imageViewLanyaMy.frame;
            
            _labelOfflineNumMy.text = [NSString stringWithFormat:@"%ld",model.offine_num.integerValue];
        }

    }
    
    
    if (![BGFunctionHelper isNULLOfString:model.mima_num]) {
        if (model.mima_num.integerValue == 0) {
            _labelMimaMy.hidden = YES;
            _imageViewMimaMy.hidden = YES;

            _labelLanyaMy.frame = _labelMimaMy.frame;
            _imageViewLanyaMy.frame = _imageViewMimaMy.frame;
        }else{

            _labelMimaMy.hidden = NO;
            _imageViewMimaMy.hidden = NO;
            
            _labelLanyaMy.frame = _labelLanyaMy.frame;
            _imageViewLanyaMy.frame = _imageViewLanyaMy.frame;
            _labelMimaMy.text = [NSString stringWithFormat:@"%ld",model.mima_num.integerValue];
        }

    }
    
    
    if (![BGFunctionHelper isNULLOfString:model.sub_count]) {
        _labelLanyaMy.text = [NSString stringWithFormat:@"%ld (今)",model.sub_count.integerValue];
    }
    
    
    [_imageViewHeader sd_setImageWithURL:[NSURL URLWithString:model.img_url] placeholderImage:[UIImage imageNamed:@""]];
}

- (void)creatNewImageViewAndLabels{

    _imageViewAllNumMy = [self creatNewImageViewByNewImageView:_imageViewAllNumMy oldImageView:_imageViewAllNum];
    
    _imageViewOnlineNumMy = [self creatNewImageViewByNewImageView:_imageViewOnlineNumMy oldImageView:_imageViewOnlineNum];
    
    _imageViewOfflineNumMy = [self creatNewImageViewByNewImageView:_imageViewOfflineNumMy oldImageView:_imageViewOfflineNum];
    
    _imageViewMimaMy = [self creatNewImageViewByNewImageView:_imageViewMimaMy oldImageView:_imageViewMima];
    
    _imageViewLanyaMy = [self creatNewImageViewByNewImageView:_imageViewLanyaMy oldImageView:_imageViewLanya];

    _labelAllNumMy = [self creatNewLabelByNewLabel:_labelAllNumMy oldLabel:_labelAllNum];
    _labelOnlineNumMy = [self creatNewLabelByNewLabel:_labelOnlineNumMy oldLabel:_labelOnlineNum];
    _labelOfflineNumMy = [self creatNewLabelByNewLabel:_labelOfflineNumMy oldLabel:_labelOfflineNum];
    _labelMimaMy = [self creatNewLabelByNewLabel:_labelMimaMy oldLabel:_labelMima];
    _labelLanyaMy = [self creatNewLabelByNewLabel:_labelLanyaMy oldLabel:_labelLanya];

}

- (UIImageView *)creatNewImageViewByNewImageView:(UIImageView *)imageView oldImageView:(UIImageView *)oldImageView{
    if (imageView) {
        [imageView removeFromSuperview];
        imageView = nil;
    }
    imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(kScreenSize.width - 95, oldImageView.orginY, oldImageView.sizeWidth, oldImageView.sizeHeight);
    imageView.image = oldImageView.image;
    [self.viewContent addSubview:imageView];
    
    imageView.hidden = NO;
    return imageView;
}

- (UILabel *)creatNewLabelByNewLabel:(UILabel *)label oldLabel:(UILabel *)oldLabel{
    if (label) {
        [label removeFromSuperview];
        label = nil;
    }
    label = [[UILabel alloc] init];
    label.frame = CGRectMake(kScreenSize.width - 75, oldLabel.orginY, oldLabel.sizeWidth, oldLabel.sizeHeight);
    label.font = oldLabel.font;
    [self.viewContent addSubview:label];
    label.hidden = NO;
    return label;
}

- (CGFloat)height{
    
//    return _viewContent.allHeight + _shopAddress.sizeHeight - 20;
    CGSize titleSize = [_shopAddress.text boundingRectWithSize:CGSizeMake(kScreenSize.width - 125 - 95, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
    return _viewContent.allHeight + (titleSize.height > 20 ? titleSize.height : 20) - 20;
}

- (UIViewController *)viewController {
    UIResponder *next = self.nextResponder;
    do {
        //判断响应者是否为视图控制器
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        next = next.nextResponder;
    } while (next != nil);
    
    return nil;
}

- (void)callFun{

    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",_labelMerTel.text];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [[self viewController].view addSubview:callWebview];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
